<?php
require_once('component/element.php');
require_once('component/elementInner.php');
require_once('component/pre.php');
require_once('component/label.php');
require_once('component/css.php');
require_once('component/html.php');
require_once('component/body.php');
require_once('component/head.php');
require_once('component/meta.php');
require_once('component/title.php');
require_once('component/pureHtml.php');
require_once('component/dummy.php');
require_once('component/img.php');
require_once('component/input.php');
require_once('component/text.php');
require_once('component/password.php');
require_once('component/checkbox.php');
require_once('component/hidden.php');
require_once('component/submit.php');
require_once('component/imageButton.php');
require_once('component/button.php');
require_once('component/htmlTable.php');
require_once('component/tr.php');
require_once('component/td.php');
require_once('component/select.php');
require_once('component/sselect.php');
require_once('component/option.php');
require_once('component/textarea.php');
require_once('component/a.php');
require_once('component/link.php');
require_once('component/div.php');
require_once('component/span.php');
require_once('component/form.php');
require_once('component/script.php');
require_once('component/scriptFile.php');
require_once('component/styleFile.php');
require_once('component/grid.php');
require_once('component/page.php');
require_once('htmlExtended.php');
require_once('component/htmlEasyForm.php');
require_once('sql.php');
require_once('fileSystem.php');


/**
* Get the contents of a file inside theme structure
*
* @param $file the file to get contents
*/
function getThemeContent($file, $args = null )
{
    $theme = THEME;

    if (!$theme)
    {
        die('Theme not defined. Plese make DEFINE(theme).');
    }

    $path = 'theme/'.THEME.'/'.$file ; //get inside theme folder

    if (file_exists($path))
    {
        $content = file_get_contents($path);
    }

    //if don't find get the default in component folder
    if (!$content)
    {
    	$debug_backtrace = debug_backtrace(); //try to get the last class, or the component name, if was called inside a component
        $component = $debug_backtrace[1]['class'];
        $content = file_get_contents('component/'.$component.'/'.$file);
    }

    if ($content)
    {

	    $content = utf8_decode($content); //converte para não ter problemas de acentuação

	    if ( is_array( $args ) )
	    {
	        $content = replaceContent($args, $content);
	    }

	    $pattern = '(\$[0-9A-z]*\([0-9A-z=; ]*\))'; //reconhece inscrições de componente nos htmls ex.: $component(variavel=valor)
	    preg_match_all($pattern, $content, $variables);

	    $variables = $variables[0];

	    if (is_array($variables))
	    {
	        foreach ( $variables as $line => $info)
	        {
	            $temp = explode('(', $info);

	            $component = str_replace('$', '', $temp[0] );

	            //TODO tratar argumentos de acordo fazer função para tratar acima também
	            $functionArgs = null;

	            if ( require_once( "component/$component/$component.php") )
	            {
	                $object = new $component($functionArgs);
	                $content = str_replace($info, $object->__toString(), $content);
	            }

	        }
	    }

    }

    return $content;
}

/**
* Passes trough an array replacinf $line by content
*
* @param $data the array
* #param $string thr string to replace
*/
function replaceContent($data, $string)
{
    if ( is_array($data) )
    {
        foreach ( $data as $line => $info )
        {
            $string = str_replace('$'.$line, $info, $string);
        }
    }
    return $string;
}

//todo
function explodeArgs($args)
{

}

function clog($var)
{
    $print_r = print_r($var,1);
    $print_r = addSlashes($print_r);
    $print_r = str_replace("\n", '\n', $print_r);
    $script = new script("console.log('$print_r')");
    echo $script;
}

function DEFINEJS($defineName)
{
	global $page;

	$page->addJsCode('var '.$defineName.' = '.'\''.constant($defineName).'\';');
}

/**
* Convert a PHP array to a Javascript array
*
*/
function toJSArray($PHParray, $arrayName)
{
    if (is_array($PHParray) )
    {
        //Mount the array to javaScript
        $jsArray[]=$arrayName.' = new Array('.count($PHParray).');'."\n";
        foreach ($PHParray as $line => $info)
        {
            $jsArray[]=$arrayName.'[\''.$line.'\']=\''.$info.'\';'. "\n";
        }
        $code=implode($jsArray);
        return $code;
    }
    else
    {
        return '';
    }
}

?>
